package com.gt.admin.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gt.admin.domain.Template;
import com.gt.admin.domain.TemplateData;
import com.gt.admin.domain.apiVO.TemplateVVO;
import com.gt.admin.domain.apiVO.Version;
import com.gt.admin.domain.vo.TemplateVO;
import com.gt.admin.mapper.TemplateDataMapper;
import com.gt.admin.mapper.TemplateMapper;
import com.gt.admin.service.ITemplateService;
import com.gt.admin.utils.Effect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 结构化模板service
 */
@Service
public class TemplateServiceImpl extends ServiceImpl<TemplateMapper, Template> implements ITemplateService {
    @Autowired
    private TemplateMapper templateMapper;
    @Autowired
    private TemplateDataMapper templateDataMapper;
    /**
     * 查询版本信息
     */
    @Override
    public List<Version> selectVersion(Long superId) {
        return templateMapper.selectVersion(superId);
    }

    /**
     * 按上级目录查询结构化模板列表
     */
    @Override
    public TemplateVVO selectBySuperId(Long superId) {
        Long[] tids = templateMapper.selectIds(superId);
        List<TemplateVO> list = new ArrayList<>();
        TemplateVVO templateVVO = new TemplateVVO();
        for (Long tid : tids) {
            TemplateVO templateVO = selectByTId(tid);
            list.add(templateVO);
            if (templateVO.getEffect() == Effect.Effective){
                templateVVO.setChooseVersion(templateVO.getVersion());
            }
        }templateVVO.setTemplateVOList(list);
        return templateVVO;
    }

    /**
     * 根据tempId查询结构化模板及使用明细
     */
    @Override
    public TemplateVO selectByTId(Long tempId) {
        Template template = getById(tempId);
        TemplateVO templateVO = BeanUtil.copyProperties(template, TemplateVO.class);
        LambdaQueryWrapper<TemplateData> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(TemplateData::getTempId, tempId);
        templateVO.setTemplateDataList(templateDataMapper.selectList(queryWrapper));
        return templateVO;
    }

    /**
     * 新增或修改结构化模板和使用明细，更新生效版本
     */
    @Transactional
    @Override
    public boolean update(TemplateVVO templateVVO) {
        Long version = templateVVO.getChooseVersion();
        List<TemplateVO> templateVOList = templateVVO.getTemplateVOList();
        Long superId = templateVOList.get(1).getSuperId();
        LambdaUpdateWrapper<Template> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(Template::getEffect, Effect.Invalid)
                .eq(Template::getSuperId, superId);
        update(updateWrapper);
        for (TemplateVO templateVO : templateVOList) {
            if(templateVO.getVersion().longValue() == version.longValue()){
                templateVO.setEffect(Effect.Effective);
                saveOrUpdate(templateVO);
                List<TemplateData> templateDataList = templateVO.getTemplateDataList();
                Long tempId = templateVO.getTempId();
                Long[] ids = templateDataMapper.selectByTempId(tempId);
                if (ids.length>0){
                    templateDataMapper.deleteBatchIds(Arrays.asList(ids));
                }
                for (TemplateData templateData: templateDataList) {
                    templateData.setTempId(tempId);
                }
                if (templateDataList.size()>0) {
                    templateDataMapper.insertBatchSomeColumn(templateDataList);
                }
            }
        }return true;
    }

    /**
     * 删除结构化模板和使用明细
     */
    @Transactional
    @Override
    public boolean delete(Long tempId) {
        removeById(tempId);
        Long[] ids = templateDataMapper.selectByTempId(tempId);
        templateDataMapper.deleteBatchIds(Arrays.asList(ids));
        return true;
    }
}
